package com.gt.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.app.domain.EsEnvironmentFactorInfo;
import com.gt.app.service.EsEnvironmentFactorInfoService;
import com.gt.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 环境因素信息表 前端控制器
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@RestController
@RequestMapping("esEnvironmentFactorInfo")
@Api(tags = "环境因素信息接口")
public class EsEnvironmentFactorInfoController {

    @Autowired
    private EsEnvironmentFactorInfoService esEnvironmentFactorInfoService;

    @ApiOperation(value = "新增环境因素信息")
    @PostMapping(value = "add")
    public AjaxResult add(@RequestBody EsEnvironmentFactorInfo esEnvironmentFactorInfo) {

        try {
            if (esEnvironmentFactorInfo == null){
                return null;
            }
            LambdaQueryWrapper<EsEnvironmentFactorInfo> eq = new LambdaQueryWrapper<EsEnvironmentFactorInfo>()
                    .eq(EsEnvironmentFactorInfo::getEsCheckId, esEnvironmentFactorInfo.getEsCheckId());
            EsEnvironmentFactorInfo environmentFactorInfo = esEnvironmentFactorInfoService.getOne(eq);
            if (environmentFactorInfo != null) {
                esEnvironmentFactorInfoService.remove(eq);
            }
            esEnvironmentFactorInfoService.save(esEnvironmentFactorInfo);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "根据checkId获取环境因素信息")
    @GetMapping(value = "getEnvironmentFactorInfo")
    public AjaxResult getFamilyHistory(String checkId) {
        LambdaQueryWrapper<EsEnvironmentFactorInfo> eq = new LambdaQueryWrapper<EsEnvironmentFactorInfo>().eq(EsEnvironmentFactorInfo::getEsCheckId, checkId);
        EsEnvironmentFactorInfo getEnvironmentFactorInfo = esEnvironmentFactorInfoService.getOne(eq);
        return AjaxResult.success(getEnvironmentFactorInfo);
    }

    //@ApiOperation(value = "更新环境因素信息")
    @PutMapping(value = "update")
    public AjaxResult update(@RequestBody EsEnvironmentFactorInfo esEnvironmentFactorInfo) {
        try {
            esEnvironmentFactorInfoService.updateById(esEnvironmentFactorInfo);
            return AjaxResult.success(esEnvironmentFactorInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    //@ApiOperation(value = "删除环境因素信息")
    @DeleteMapping(value = "delete")
    public AjaxResult delete(String id) {
        try {
            return AjaxResult.success(esEnvironmentFactorInfoService.removeById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
